All files / web/src/app/api/classrooms/[classroomId] route.ts

0% Statements 0/111
0% Branches 0/1
0% Functions 0/1
0% Lines 0/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                                                                                                                               
import { NextResponse } from 'next/server'
import {
  deleteClassroom,
  getClassroom,
  updateClassroom,
  regenerateClassroomCode,
} from '@/lib/classroom'
import { getUserId } from '@/lib/viewer'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/classrooms/[classroomId]
 * Get classroom by ID
 *
 * Returns: { classroom } or 404
 */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { classroomId } = (await params) as { classroomId: string }

    const classroom = await getClassroom(classroomId)

    if (!classroom) {
      return NextResponse.json({ error: 'Classroom not found' }, { status: 404 })
    }

    return NextResponse.json({ classroom })
  } catch (error) {
    console.error('Failed to fetch classroom:', error)
    return NextResponse.json({ error: 'Failed to fetch classroom' }, { status: 500 })
  }
})

/**
 * PATCH /api/classrooms/[classroomId]
 * Update classroom settings (teacher only)
 *
 * Body: { name?: string, regenerateCode?: boolean, entryPromptExpiryMinutes?: number | null }
 * Returns: { classroom }
 */
export const PATCH = withAuth(async (req, { params }) => {
  try {
    const { classroomId } = (await params) as { classroomId: string }
    const userId = await getUserId()
    const body = await req.json()

    // Handle code regeneration separately
    if (body.regenerateCode) {
      const newCode = await regenerateClassroomCode(classroomId, userId)
      if (!newCode) {
        return NextResponse.json(
          { error: 'Not authorized or classroom not found' },
          { status: 403 }
        )
      }
      // Fetch updated classroom
      const classroom = await getClassroom(classroomId)
      return NextResponse.json({ classroom })
    }

    // Update other fields
    const updates: { name?: string; entryPromptExpiryMinutes?: number | null } = {}
    if (body.name) updates.name = body.name
    // Allow setting to null (use system default) or a positive number
    if ('entryPromptExpiryMinutes' in body) {
      const value = body.entryPromptExpiryMinutes
      if (value === null || (typeof value === 'number' && value > 0)) {
        updates.entryPromptExpiryMinutes = value
      }
    }

    if (Object.keys(updates).length === 0) {
      return NextResponse.json({ error: 'No valid updates provided' }, { status: 400 })
    }

    const classroom = await updateClassroom(classroomId, userId, updates)

    if (!classroom) {
      return NextResponse.json({ error: 'Not authorized or classroom not found' }, { status: 403 })
    }

    return NextResponse.json({ classroom })
  } catch (error) {
    console.error('Failed to update classroom:', error)
    return NextResponse.json({ error: 'Failed to update classroom' }, { status: 500 })
  }
})

/**
 * DELETE /api/classrooms/[classroomId]
 * Delete classroom (teacher only, cascades enrollments)
 *
 * Returns: { success: true }
 */
export const DELETE = withAuth(async (_request, { params }) => {
  try {
    const { classroomId } = (await params) as { classroomId: string }
    const userId = await getUserId()

    const success = await deleteClassroom(classroomId, userId)

    if (!success) {
      return NextResponse.json({ error: 'Not authorized or classroom not found' }, { status: 403 })
    }

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Failed to delete classroom:', error)
    return NextResponse.json({ error: 'Failed to delete classroom' }, { status: 500 })
  }
})